<?php
namespace shareTrait\Model;
/**
 * Description of ExtraFunction
 *
 * @author hardy
 */
trait GetData
{
    /********************查询类型************************/
    /**
     * 反射
     * @param $function
     * @return bool
     * @throws \ReflectionException
     */
    public function checkUnusual($function){
        $className = new \ReflectionClass($this);
        return method_exists($className->name, $function);
    }

    /**
     * 获取分页
     * @param $map
     * @param $page
     * @param $page_nums
     * @param string $order
     * @return mixed
     * @throws \ReflectionException
     */
    public function getListForPageData($map, $page, $page_nums, $order = '')
    {
        if($order != ''){
            $this->order($order);
        }
        $data =  $this->where($map)->page($page, $page_nums)->select();
        if($this->checkUnusual('JoinData')){
            foreach ($data as &$item){
                $this->setJoinData($item);
            }
        }
        return $data;
    }

    /**
     * 获取分页
     * @param $map
     * @param $page
     * @param $page_nums
     * @param string $order
     * @return mixed
     * @throws \ReflectionException
     */
    public function getListForPageDataSimple($map, $page, $page_nums, $order = '')
    {
        if($order != ''){
            $this->order($order);
        }
        $data =  $this->where($map)->page($page, $page_nums)->select();
        if($this->checkUnusual('JoinSimple')){
            foreach ($data as &$item){
                $this->setJoinDataSimple($item);
            }
        }
        return $data;
    }
    /**
     * 获取一条数据
     * @param $id
     * @return array
     * @throws \ReflectionException
     */
    public function getOneData($id){
        if(empty($id)){
            return [];
        }
        if(is_array($id)){
            $data = $this->where($id)->find();
        }else{
            $data = $this->find($id);
        }
        if(empty($data)){
            return [];
        }
        if ($this->checkUnusual('JoinData')){
            $this->setJoinData($data);
        }
        return $data;
    }
    /********************删除类型************************/
}
